

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
 *
 * @author CUIJIAJUN
 *
 * 2015-6-1
 *
 */
public class Ftp {
    /**
     * 本地文件
     */
    private String localFileName;
    /**
     * 远程文件
     */
    private String remoteFileName;
    /**
     * FTP客户端
     */
    private FtpClient ftpClient;
 
    /**
     * 服务器连接
     * @param ip 服务器IP
     * @param port 服务器端口
     * @param user 用户名
     * @param password 密码
     * @param path 服务器路径
     */
    public void connectServer(String ip, int port, String user,
            String password, String path) {
        try {
            /* ******连接服务器的两种方法*******/
            //第一种方方式
//            ftpClient = new FtpClient();
//            ftpClient.openServer(ip, port);
            //第二种方式
            ftpClient = new FtpClient(ip);
            ftpClient.login(user, password);
            // 设置二进制传输
            ftpClient.binary();
            System.out.println("用户"+user+"登陆成功！");
            if (path.length() != 0){
                //把远程系统上的目录切换到参数path指定的目录
                ftpClient.cd(path);
            }
            ftpClient.binary();
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    }
    /**
     * 关闭连接
     */
    public void closeConnect() {
        try {
            ftpClient.closeServer();
            System.out.println("断开连接成功！");
        } catch (IOException ex) {
            System.out.println("无法断开！");
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    }
    /**
     * 上传文件
     * @param localFile 本地文件
     * @param remoteFile 远程文件
     */
    public void upload(String localFile, String remoteFile) {
        this.localFileName = localFile;
        this.remoteFileName = remoteFile;
        
        TelnetOutputStream os = null;
        FileInputStream is = null;
        try {
            //将远程文件加入输出流
            os = ftpClient.put(new String(remoteFileName.getBytes("gbk"),"iso-8859-1"));
            //获取本地文件的输入流
            File file_in = new File(localFileName);
            is = new FileInputStream(file_in);
            //创建缓冲
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            System.out.println("上传成功！");
        } catch (IOException ex) {
            System.out.println("上传失败！");
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally{
            try {
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(os != null){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
     
    /**
     * 下载文件
     * @param remoteFile 远程文件路径(服务器端)
     * @param localFile 本地文件路径(客户端)
     */
    public void download(String remoteFile, String localFile) {
        TelnetInputStream is = null;
        FileOutputStream os = null;
        try {
            //获取远程机器上的文件文件，并且写到本地
            is = ftpClient.get(remoteFile);
            File file_in = new File(localFile);
            if(!file_in.exists()){
            	File fileDir=new File(file_in.getParent());  
                if(!fileDir.exists()){  
                	fileDir.mkdirs();  //创建文件夹  
                }
                file_in.createNewFile();
            }
            os = new FileOutputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            System.out.println("文件："+file_in.getName()+"下载成功！");
        } catch (IOException ex) {
            System.out.println("下载失败！");
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally{
            try {
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(os != null){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
 
    public static void main(String agrs[]) {
 
        String ftpFilePath[] = { "TLFM01.txt", "TLFM01"};
        String locFilePath[] = { "C:\\tmpcjj\\我的1.txt","C:\\tmpcjj\\我的2.txt"};
 
        Ftp fu = new Ftp();
        /*
         * 使用配置的端口号、用户名、密码以及根目录连接FTP服务器
         */
        InputStream inStream = Ftp.class.getClassLoader().getResourceAsStream("ftp.properties");;
    	Properties properties = new Properties();
		try {
			properties.load(inStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String ip = properties.getProperty("ftpHost"); 
		int port = Integer.parseInt(properties.getProperty("ftpPort")); 
		String user = properties.getProperty("ftpUserName"); 
		String password = properties.getProperty("ftpPassword"); 
		String path = properties.getProperty("downloadFtpPath");
        fu.connectServer(ip, port, user, password, path);
        
//        //下载
        for (int i = 0; i < ftpFilePath.length; i++) {
            fu.download(ftpFilePath[i], locFilePath[i]);
        }
        
//        String localFile = "j:\\5月工作报告.xls";
//        String remoteFile = "/downTemp/哈哈.xls";
//        //上传
//        fu.upload(localFile, remoteFile);
        fu.closeConnect();
    }
}
